package com.tianji.aigc.tools;

import cn.hutool.core.convert.Convert;
import com.tianji.aigc.config.ToolResultHolder;
import com.tianji.aigc.constants.Constants;
import com.tianji.aigc.tools.result.CourseInfo;
import com.tianji.api.client.course.CourseClient;
import lombok.RequiredArgsConstructor;
import org.springframework.ai.chat.model.ToolContext;
import org.springframework.ai.tool.annotation.Tool;
import org.springframework.ai.tool.annotation.ToolParam;
import org.springframework.stereotype.Component;

import java.util.Optional;

/**
 * @author Administrator
 * @version 1.0
 * @description: TODO
 * @date 2025/7/23 10:06
 */
@Component
@RequiredArgsConstructor
public class CourseTool {
    private final CourseClient courseClient;

    @Tool(description = Constants.Tools.QUERY_COURSE_BY_ID)
    public CourseInfo queryCourseById(@ToolParam(description = Constants.ToolParams.COURSE_ID) Long courseId,
                                      ToolContext toolContext) {
        /*if (ObjectUtil.isNull(courseId)) {
            return null;
        }
        CourseBaseInfoDTO basedInfo = courseClient.baseInfo(courseId, true);
        return CourseInfo.of(basedInfo);*/

        return Optional.ofNullable(courseId)
                .map(id -> CourseInfo.of(courseClient.baseInfo(id, true)))// 远程调用查询课程基本信息
                .map(courseInfo -> {  // 将 Tool 工具中的查询结果放入工具结果保持器中，便于后续使用（课程卡片）
                    String requestId = Convert.toStr(toolContext.getContext().get(Constants.REQUEST_ID)); // 会话请求ID
                    String field = "couseInfo_" + courseId; // 课程ID
                    ToolResultHolder.put(requestId,field,courseInfo); // 放入工具结果保持器中， 会话请求ID：课程ID：课程信息
                    return courseInfo;
                })
                .orElse(null);
    }
}
